# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui

#_______________________________________________________________________________
class PlaylistTreeView(QtGui.QTreeView):
    #TODO: sistemare il cursore per il drop (dropcursor)
    playlist_cleared = QtCore.pyqtSignal()
    songs_deleted = QtCore.pyqtSignal(list)
    song_doubleclicked = QtCore.pyqtSignal(int)
#    moved_songs = QtCore.pyqtSignal((list, int))
#    added_songs = QtCore.pyqtSignal((list, int))
        
    def __init__(self, parent):
        QtGui.QTreeView.__init__(self, parent)
        self.setEnabled(False)
        self.set_context_menu(self)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)
        self.dropRect = None
        
    def set_context_menu(self, parent):
        self.context_menu = QtGui.QMenu("Context Menu", parent)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/images/clear.png"), 
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.clear_action = self.context_menu.addAction(icon, "Clear")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/images/delete.png"), 
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.delete_action = self.context_menu.addAction(icon, "Delete Selection")
          
    def setModel(self, model):
        self.model = model
#        self.setEnabled(True)
        QtGui.QTreeView.setModel(self, model)
        self.clear_action.triggered.connect(self.playlist_cleared.emit)
        self.delete_action.triggered.connect(self.delete_action_triggered)
    
    def delete_action_triggered(self):
        list_idx = self.selectedIndexes()
        if list_idx:
            self.songs_deleted.emit(list_idx)
            self.clearSelection()
    
#    def dragMoveEvent(self, me):
#        self.dropRect = me.answerRect()
#        self.model.layoutChanged.emit()
#    
#    def paintEvent(self, pe):
#        # FIXME: cambiare indicatore di drop
#        QtGui.QTreeView.paintEvent(self, pe)
#        if not self.dropRect is None: 
#            painter = QtGui.QPainter(self.viewport())
#            x,y,w,h = self.dropRect.getRect()
#            self.dropRect = None
#            point = QtCore.QPoint(x,y)
#            idx = self.indexAt(point)
#            if idx.row() == -1:
#                row = self.model.rowCount() - 1
#                idx = self.model.index(row, 0, QtCore.QModelIndex())
#                arect = self.visualRect(idx)
#                b = arect.y() + self.rowHeight(idx)
#            else:
#                arect = self.visualRect(idx)
#                b = arect.y()
#            brush = QtGui.QBrush(QtCore.Qt.black, QtCore.Qt.Dense4Pattern)
#            pen = QtGui.QPen()
#            pen.setWidth(2)
#            pen.setBrush(brush)
#            painter.setPen(pen)    
#            painter.drawLine(0, b, self.width(), b)
#            pe.accept()
    
    def mouseDoubleClickEvent(self, me):
        QtGui.QTreeView.mouseDoubleClickEvent(self, me)
        if (me.button() == QtCore.Qt.LeftButton and 
            me.modifiers() == QtCore.Qt.NoModifier):
                index = self.indexAt(me.pos())
                if not index.isValid():
                    return
                index = index.sibling(index.row(), 1)
                id = index.data().toInt()[0]
                self.song_doubleclicked.emit(id)
                
    def mousePressEvent(self, me):
        QtGui.QTreeView.mousePressEvent(self, me)
        if (me.button() == QtCore.Qt.RightButton and 
            me.modifiers() == QtCore.Qt.NoModifier):
            self.context_menu.popup(me.globalPos())
